package com.whalehearing.tingyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whalehearing.tingyou.domain.Article;
import com.whalehearing.tingyou.mapper.ArticleMapper;
import com.whalehearing.tingyou.service.IArticleService;
import com.whalehearing.tingyou.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 * 文章Service业务层处理
 *
 * @author zengjun-qxm
 * @date 2024-08-05
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 查询文章列表
     *
     * @param article 文章
     * @return 文章
     */
    @Override
    public List<Article> selectArticleList(Article article)
    {
        List<Article> articleList = articleMapper.selectList(buildQueryWrapper(article));
        return articleList;
    }


    private LambdaQueryWrapper<Article> buildQueryWrapper(Article query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<Article> lqw = Wrappers.lambdaQuery();
        lqw.eq(query.getArticleId() != null, Article::getArticleId, query.getArticleId());
        lqw.like(StringUtils.isNotBlank(query.getTitle()), Article::getTitle, query.getTitle());
        lqw.like(StringUtils.isNotBlank(query.getContent()), Article::getContent, query.getContent());
        lqw.eq(StringUtils.isNotBlank(query.getCoverImg()), Article::getCoverImg, query.getCoverImg());
        lqw.eq(StringUtils.isNotBlank(query.getState()), Article::getState, query.getState());
        lqw.like(query.getCategoryId() != null, Article::getCategoryId, query.getCategoryId());
        lqw.eq(query.getCreateById() != null, Article::getCreateById, query.getCreateById());
        lqw.like(StringUtils.isNotBlank(query.getCreateBy()), Article::getCreateBy, query.getCreateBy());
        lqw.eq(query.getCreateTime() != null, Article::getCreateTime, query.getCreateTime());
        lqw.orderByDesc(Article::getCreateTime);
        lqw.like(StringUtils.isNotBlank(query.getUpdateBy()), Article::getUpdateBy, query.getUpdateBy());
        lqw.eq(query.getUpdateTime() != null, Article::getUpdateTime, query.getUpdateTime());
        lqw.like(StringUtils.isNotBlank(query.getRemark()), Article::getRemark, query.getRemark());
        lqw.eq(query.getIsDeleted() != null, Article::getIsDeleted, query.getIsDeleted());
        return lqw;
    }

}
